package com.zxjbyte.yiyi.framework.common.core.pdf;

import com.itextpdf.text.*;
import com.itextpdf.text.pdf.BaseFont;

import java.io.IOException;

/**
 * @Author zhangxingjia
 * @Date 2023/3/24 14:04
 * @Version: 1.0
 */
public class PdfHelper {

    public static final float DOCUMENT_MARGIN_LEFT = 20;
    public static final float DOCUMENT_MARGIN_RIGHT = 20;
    public static final float DOCUMENT_MARGIN_TOP = 30;
    public static final float DOCUMENT_MARGIN_BOTTOM = 40;
    public static final Rectangle PAGE_SIZE_A4 = PageSize.A4;

    /**
     * 获取中文基础字体
     * @return
     */
    public static BaseFont getChineseBaseFont() {
        return BaseFontHolder.baseFont;
    }

    private static final class BaseFontHolder {
        static BaseFont baseFont = null;
        static {
            try {
                baseFont = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
            } catch (DocumentException | IOException ignored) {
            }
        }
    }

    /**
     * 创建字体
     * @param size
     * @param color
     * @return
     */
    public static Font createFont(float size, BaseColor color){
        return new Font(PdfHelper.getChineseBaseFont(), size, Font.NORMAL, color);
    }

    /**
     * 创建默认字体
     * @return
     */
    public static Font createDefaultFont(){
        return PdfHelper.createFont(12, BaseColor.BLACK);
    }

    /**
     * 获取pdf文档
     * @return
     */
    public static Document getDocument(){
        //页面大小
        Rectangle rectangle = new Rectangle(PAGE_SIZE_A4);
        //页面背景色
        rectangle.setBackgroundColor(BaseColor.WHITE);
        //创建文档
        Document document = new Document(rectangle);
        //页边空白
        document.setMargins(DOCUMENT_MARGIN_LEFT, DOCUMENT_MARGIN_RIGHT, DOCUMENT_MARGIN_TOP, DOCUMENT_MARGIN_BOTTOM);
        return document;
    }

}
